package com.example.Controller;

import com.atguigu.model.process.Process;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.process.ProcessType;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.process.ProcessFormVo;
import com.atguigu.vo.process.ProcessQueryVo;
import com.atguigu.vo.process.ProcessVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Result;
import com.example.ResultCodeEnum;
import com.example.Service.ProcessTemplateService;
import com.example.Service.SysProcessService;
import com.example.Service.SysProcessTypeService;
import com.example.Service.SysUserService;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

@RequestMapping("/admin/process")
@RestController
public class ProcessController {
    @Resource
    private SysProcessTypeService sysProcessTypeService;
    @Resource
    private ProcessTemplateService processTemplateService;
    @Resource
    private SysProcessService sysProcessService;
  @Resource
  private SysUserService sysUserService;
    @GetMapping("/processType/{page}/{limit}")
    public Result processTypelist(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit){
        Page<ProcessType> page1=new Page<>();
        IPage<ProcessType> page2=sysProcessTypeService.page(page1);

        return Result.build(page2, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/processType/save")
    public Result save(@RequestBody ProcessType processType){
        sysProcessTypeService.save(processType);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/processType/get/{id}")
    public Result get(@PathVariable("id") Long id){
        ProcessType processType=sysProcessTypeService.getById(id);
        return Result.build(processType,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/processType/update")
    public Result update(@RequestBody ProcessType processType){
        sysProcessTypeService.updateById(processType);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/processType/remove/{id}")
    public Result remove(@PathVariable("id") Integer id){
        sysProcessTypeService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/processTemplate/{page}/{limit}")
    public Result processTemplatelist(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit){
        Page<ProcessTemplate> page1=new Page<>();
        IPage<ProcessTemplate> page2=processTemplateService.page(page1);
     for (ProcessTemplate processTemplate:page2.getRecords()){

         processTemplate.setProcessTypeName(sysProcessTypeService.getById(processTemplate.getProcessTypeId()).getName());
     }
        return Result.build(page2, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/processType/findAll")
    public Result processTypefindAll(){
        return Result.build(sysProcessTypeService.list(),ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/processTemplate/save")
    public Result processTemplatesave(@RequestBody ProcessTemplate processTemplate){
        processTemplateService.save(processTemplate);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/processTemplate/get/{id}")
    public Result getprocessTemplate(@PathVariable("id") Long id){
        ProcessTemplate processTemplate=processTemplateService.getById(id);
        return Result.build(processTemplate,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/processTemplate/update")
    public Result processTemplateupdate(@RequestBody ProcessTemplate processTemplate){
        processTemplateService.updateById(processTemplate);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/processTemplate/remove/{id}")
    public Result processTemplateromove(@PathVariable("id") Integer id){
        processTemplateService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/processTemplate/uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file) throws FileNotFoundException{
        String path=new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();
        String fileName=file.getOriginalFilename();
        File template=new File(path+"/processes/");
        if(!template.exists()){
            template.mkdirs();
        }
        File imageFile=new File(path+"/processes"+fileName);
        Map<String,Object> map=new HashMap<>();
        map.put("processDefinitionPath","processes/"+fileName);
        map.put("processDefinitionKey",fileName.substring(0,fileName.lastIndexOf(".")));
        return Result.ok(map);


    }
    @GetMapping("/processTemplate/publish/{id}")
    public Result publish(@PathVariable("id") Integer id){
        ProcessTemplate processTemplate=processTemplateService.getById(id);
        processTemplate.setStatus(1);
        processTemplateService.updateById(processTemplate);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/{page}/{limit}")
    public Result page(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, ProcessQueryVo processQueryVo){
        Page<Process> page1=new Page<>();
        LambdaQueryWrapper<Process> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(processQueryVo.getKeyword())){
            lambdaQueryWrapper.eq(Process::getProcessCode,processQueryVo.getKeyword());

        }
        if(!StringUtils.isEmpty(processQueryVo.getStatus())){
            lambdaQueryWrapper.eq(Process::getStatus,processQueryVo.getStatus());

        }
        if(!StringUtils.isEmpty(processQueryVo.getCreateTimeBegin())){
            lambdaQueryWrapper.gt(Process::getCreateTime,processQueryVo.getCreateTimeBegin());

        }
        if(!StringUtils.isEmpty(processQueryVo.getCreateTimeEnd())){
            lambdaQueryWrapper.lt(Process::getCreateTime,processQueryVo.getCreateTimeEnd());

        }
        IPage<Process> page2=sysProcessService.page(page1,lambdaQueryWrapper);
        for (Process process:page2.getRecords()){

            ProcessTemplate processTemplate=processTemplateService.getById(process.getProcessTemplateId());
            process.setProcessTemplateName(processTemplate.getName());
            ProcessType processType=sysProcessTypeService.getById(process.getProcessTypeId());
            process.setProcessTypeName(processType.getName());
            SysUser user=  sysUserService.getById(process.getUserId());
            process.setName(user.getName());

        }

        return Result.build(page2,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/findProcessType")
    public Result findProcessType(){
        List<ProcessType> list=sysProcessTypeService.list(null);
        for (ProcessType processType:list){
            Long typeId=processType.getId();
            LambdaQueryWrapper<ProcessTemplate> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ProcessTemplate::getProcessTypeId,typeId);
            List<ProcessTemplate> list1=processTemplateService.list(lambdaQueryWrapper);
            processType.setProcessTemplateList(list1);
        }
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/getProcessTemplate/{id}")
    public Result getProcessTemplate(@PathVariable("id") Integer id){
        ProcessTemplate processTemplate=processTemplateService.getById(id);
        return Result.build(processTemplate,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo,@RequestHeader("token") String token){
//        processTemplateService.startup(processFormVo,token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
